package co.edu.javeriana.aes.patrones.monopolio.cliente.chat;

import co.edu.javeriana.aes.patrones.monopolio.cliente.AbstractPanel;
import co.edu.javeriana.aes.patrones.monopolio.cliente.MonopolyClient;
import co.edu.javeriana.aes.patrones.monopolio.cliente.ViewHandler;
import co.edu.javeriana.aes.patrones.monopolio.message.Message;
import co.edu.javeriana.aes.patrones.monopolio.message.MessageEventListener;
import co.edu.javeriana.aes.patrones.monopolio.message.MessageType;
import co.edu.javeriana.aes.patrones.monopolio.util.exception.MonopolyException;
import java.awt.event.KeyEvent;

/**
 *
 * @author gersua
 */
public class ChatJPanel extends AbstractPanel implements MessageEventListener {

    /** Creates new form ChatJPanel */
    public ChatJPanel(MonopolyClient monopolyClient, ViewHandler viewHandler) throws MonopolyException {
        super(monopolyClient, viewHandler);
        monopolyClient.addListener(this);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtChatArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtChatText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        btnSend = new javax.swing.JButton();

        setBackground(new java.awt.Color(0, 0, 0));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        txtChatArea.setColumns(20);
        txtChatArea.setEditable(false);
        txtChatArea.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        txtChatArea.setForeground(java.awt.Color.blue);
        txtChatArea.setLineWrap(true);
        txtChatArea.setRows(5);
        txtChatArea.setWrapStyleWord(true);
        jScrollPane1.setViewportView(txtChatArea);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtChatText.setColumns(20);
        txtChatText.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        txtChatText.setLineWrap(true);
        txtChatText.setRows(5);
        txtChatText.setWrapStyleWord(true);
        txtChatText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtChatTextKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(txtChatText);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/javeriana/aes/patrones/monopolio/cliente/resources/monopoly-logo.png"))); // NOI18N

        btnSend.setText("Enviar!");
        btnSend.setEnabled(false);
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(9, 9, 9)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 345, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(btnSend, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(11, 11, 11)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, 0, 0, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jLabel1)
                .add(5, 5, 5)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 268, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .add(btnSend, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void sendChatText() {
        if (!txtChatText.getText().trim().equals("")) {
            txtChatArea.append(txtChatText.getText() + "\n");
            /******************************************************************/
            Message message = new Message(getMonopolyClient().getSession(), txtChatText.getText(), MessageType.C_CHAT_MESSAGE);
            getMonopolyClient().sendMessage(message);
            /******************************************************************/
            txtChatText.setText("");
            txtChatText.requestFocus();
            btnSend.setEnabled(false);
        }
    }

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed

        sendChatText();
    }//GEN-LAST:event_btnSendActionPerformed

    private void txtChatTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtChatTextKeyReleased

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (!txtChatText.getText().trim().equals("")) {
                sendChatText();
            } else {
                txtChatText.setText("");
            }
        }

        btnSend.setEnabled(!txtChatText.getText().trim().equals(""));
    }//GEN-LAST:event_txtChatTextKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea txtChatArea;
    private javax.swing.JTextArea txtChatText;
    // End of variables declaration//GEN-END:variables

    @Override
    public void handleChatMessage(Message message) {
        txtChatArea.append(message.getSession().getNickmame() + ":" + message.getData() + "\n");
        txtChatArea.setCaretPosition(txtChatArea.getDocument().getLength());
    }

    @Override
    public void handleRooms(Message message) {
    }

    @Override
    public void handlePlayerRequest(Message message) {
    }
}
